﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmMttoTipoPago : Form
    {
        [Dependency]
        public ITipoPagoBL tipPag { get; set; }

        bool bandera;

        public FrmMttoTipoPago()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public TIPO_PAGO CurrentTipoPago { get; set; }

        public void Nuevo()
        {
            bandera = true;
            tIPO_PAGOBindingSource.AddNew();
        }

        public void Editar()
        {
            tIPO_PAGOBindingSource.DataSource = CurrentTipoPago;
        }

        private void FrmMttoTipoPago_Load(object sender, EventArgs e)
        {

        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            TIPO_PAGO currentTipoPago = tIPO_PAGOBindingSource.Current as TIPO_PAGO;
            var msg = "ERROR";
            if (currentTipoPago != null)
            {
                if (bandera == true)
                {
                    try
                    {
                        currentTipoPago.Descripcion = descripcionTextBox.Text.ToUpper();
                        tipPag.AddTipoPago(currentTipoPago);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (bandera == false)
                {
                    try
                    {
                        currentTipoPago.Descripcion = descripcionTextBox.Text.ToUpper();
                        tipPag.UpdateTipoPago(currentTipoPago);
                        MessageBox.Show("Modificación Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
